package com.i2works.smartluolong.manage.controller.user;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.URLConstant;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.web.entity.user.UserMenuDO;
import com.i2works.smartluolong.web.entity.user.UserMenuQuery;
import com.i2works.smartluolong.web.service.user.IUserMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 会员菜单
 * 说明：
 * 主要对前台页面的一些特殊页面（分角色显示）的操作
 *
 * @author Liu Yutao
 * @className UserResController
 * @email koal@vip.qq.com
 * @date 2016/2/26 14:30
 */

@Controller
@RequestMapping("/userMenu")
public class UserMenuController extends BaseModule {

 
	@Autowired
	private IUserMenuService userMenuService;

	/**
	 * 会员菜单管理页面
	 */
	@GetMapping(value="/view/index")
	public String index(){
		logger.info("会员菜单管理页面");
		return URLConstant.User.MENU_URL;
	}

	/**
	 * 菜单列表
	 */
	@GetMapping(value="/view/api")
	@ResponseBody
	public Result queryList(@ModelAttribute UserMenuQuery query) {
			Result result = new Result();
			try {
				int total = userMenuService.queryCount(query);
				List<UserMenuDO> queryUserMenuList = Lists.newArrayList();
				if (total >0) {
					queryUserMenuList = userMenuService.queryList(query);
				}
				result.setTotal(total);
				result.setRows(queryUserMenuList);
			} catch (Exception e) {
				logger.error("",e);
				result.setSuccess(false);
				result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
			}
			return result;
		}

	/**
	 * 查询分类菜单树（只显示类型是1的目录菜单）
	 */
	@GetMapping(value="/view/classTree")
	@ResponseBody
	public Result queryClassTree() {
		Result result = new Result();
		try {
			UserMenuQuery query = new UserMenuQuery();
			query.setMenuType(1);
			List<UserMenuDO> queryUserMenuList = userMenuService.queryAllList(query);
			result.setRows(queryUserMenuList);
		} catch (Exception e) {
			logger.error("",e);
			result.setSuccess(false);
			result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
		}
		return result;
	}
	

	/**
	 * 新增编辑会员菜单跳转
	 */
	@GetMapping(value="/save/form")
	public String savePage(){
		logger.info("会员菜单新增编辑");
		return URLConstant.User.MENU_FORM_URL;
	}
	
	
	/**
	 * 添加会员菜单
	 */
	@PostMapping("/save/action")
	@ResponseBody
	public Result save(@ModelAttribute UserMenuDO userMenu){
		Result result = new Result();
		try {
			userMenu.setUpdateName(getUserName());
			if (userMenu.getPid() == null) {
				userMenu.setPid(0);
			}
			userMenuService.save(userMenu);
		} catch (Exception e) {
			logger.error("", e);
			result.setSuccess(false);
			result.setMsg(MsgEnum.SAVE_ERROR.getMsg());
		}    
		return result;
	}
 
	/**
	 * 查询某个菜单
	 */
	@GetMapping("/view/{id}")
	@ResponseBody
	public Result query(@PathVariable Integer id){
		Result result = new Result();
		try {
			UserMenuQuery query = new UserMenuQuery();
			query.setId(id);
			UserMenuDO userMenu = userMenuService.query(query);
			result.setModel(userMenu); 
		} catch (Exception e) {
			logger.error("", e);
			result.setSuccess(false); 
			result.setMsg(MsgEnum.FIND_ERROR.getMsg());
		}    
		return result;
	}

	
	/**
	 * 某个会员菜单（名称、标示、代码）是否存在验证
	 */
	@PostMapping("/view/check")
	@ResponseBody
	public boolean queryCheck(@ModelAttribute UserMenuQuery query){
		boolean result = false;
		try {
			int count = userMenuService.queryCheck(query);
			 if (count<=0) {
				 result = true;
			}
		} catch (Exception e) {
			logger.error("",e);
		}    
		return result;
	}
	
	/**
	 * 更新菜单状态
	 */
	@PostMapping("/status/action")
	@ResponseBody
	public Result updateStatus(@ModelAttribute UserMenuDO userMenu){
		Result result = new Result();
		try {
			userMenu.setUpdateName(getUserName());
			userMenuService.updateStatus(userMenu);
		} catch (Exception e) {
			logger.error("",e);
			result.setSuccess(false);
			result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
		}    
		return result;
	}


	/**
	 * 更新菜单状态
	 */
	@PostMapping("/status/batchAction")
	@ResponseBody
	public Result updateBatchStatus(@RequestParam("idsList[]") List<Integer> idsList, Integer status) {
		Result result = new Result();
		try {
			if (idsList != null && idsList.size() > 0) {
				Map<String,Object> map = Maps.newHashMap();
				map.put("idsList", idsList);
				map.put("status", status);
				map.put("updateName", getUserName());
				userMenuService.updateBatchStatus(map);
			}
		} catch (Exception e) {
			logger.error("", e);
			result.setSuccess(false);
			result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
		}
		return result;
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete/action")
	@ResponseBody
	public Result delete(@RequestParam Integer id) {
		Result result = new Result();
		try {
			userMenuService.delete(id);
		} catch (Exception e) {
			logger.error("", e);
			result.setSuccess(false);
			result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
		}
		return result;
	}

	/**
	 * 批量删除
	 */
	@PostMapping("/delete/batchAction")
	@ResponseBody
	public Result deleteBatch(@RequestParam("idsList[]") List<Integer> idsList) {
		Result result = new Result();
		try {
			if (idsList != null && idsList.size() > 0) {
				userMenuService.deleteBatch(idsList);
			}
		} catch (Exception e) {
			logger.error("", e);
			result.setSuccess(false);
			result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
		}
		return result;
	}

}
